package com.oyc.demo.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.oyc.demo.domain.Cablebasic;
import com.oyc.demo.domain.Initdata;
import com.oyc.demo.mapper.CablebasicMapper;
import com.oyc.demo.mapper.InitdataMapper;
import com.oyc.demo.service.InitdataService;

/**
 * (Cablebasic)表服务实现类
 *
 * @author 杜学森
 * @since 2021-01-20 18:51:53
 */
@Service
public class InitdataServiceImpl implements InitdataService {

	@Autowired
	private InitdataMapper initdataMapper;

	@Autowired
	private CablebasicMapper cablebasicMapper;

	@Override
	public void insertBatch(List<Initdata> insert) {
		initdataMapper.insertBatch(insert);
	}

	@Override
	public void deleteById() {
		// TODO Auto-generated method stub
		initdataMapper.deleteById();
	}

	@Override
	@Transactional
	public JSONObject calculate(String provinceName,String tablename) {
		// TODO Auto-generated method stub
		JSONObject obj = new JSONObject();

		// 设置省份信息
		Initdata initdata = new Initdata();
		initdata.setProvincecompanyname(provinceName);
		initdata.setTablename(tablename);
		// 设置省份信息
		Cablebasic cablebasic = new Cablebasic();
		cablebasic.setProvincecompanyname(provinceName);
		cablebasic.setTablename(tablename);
		
		// 先去表里面查一下，有没有数据
		int count = initdataMapper.count();

		if (count > 0) {
			// 进行统计分析
			obj.put("provincecompanyname", initdataMapper.provincecompanyname(initdata));
			obj.put("count", count);
			obj.put("update", initdataMapper.mergeupdate(initdata));
			obj.put("delete", initdataMapper.mergedelete(initdata));
			obj.put("insert", initdataMapper.mergeinsert(initdata));
			// 后操作住主表
			cablebasicMapper.updatebyothertable(cablebasic);
			cablebasicMapper.insertbyothertable(cablebasic);
			cablebasicMapper.deletebyothertable(cablebasic);
			// 删除临时表
			initdataMapper.deleteById();
		} else {
			obj.put("provincecompanyname", "无");
			obj.put("count", "0");
			obj.put("update", "0");
			obj.put("delete", "0");
			obj.put("insert", "0");
		}

		return obj;
	}
}